package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.wubang.bo.OrderCommentBO;
import com.cskaoyan.bean.wubang.bo.OrderSubmitBO;
import com.cskaoyan.bean.wubang.bo.OrderWxBaseParam;
import com.cskaoyan.bean.wubang.vo.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.OrderWxService;
import com.cskaoyan.util.wubang.OrderStatusTextUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @auther: wubang
 * @description:
 * @date: 2022-06-29 10:40
 */
@Service
@Transactional
public class OrderWxServiceImpl implements OrderWxService {

    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCartMapper marketCartMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketGrouponMapper marketGrouponMapper;
    @Autowired
    MarketGrouponRulesMapper marketGrouponRulesMapper;
    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;
    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public CommonData<OrderWxListVO> selectOrderList(BaseParam baseParam, Integer showType) {
        // 开启分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        ArrayList<OrderWxListVO> orderWxListVOS = new ArrayList<>();
        if (showType == 0) {
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
            for (MarketOrder marketOrder : marketOrders) {
                OrderWxListVO orderWxListVO = new OrderWxListVO();
                orderWxListVO.setId(marketOrder.getId());
                orderWxListVO.setActualPrice(marketOrder.getActualPrice());
                orderWxListVO.setAftersaleStatus(marketOrder.getAftersaleStatus());
                orderWxListVO.setIsGroupin(false);
                orderWxListVO.setOrderSn(marketOrder.getOrderSn());
                orderWxListVO.setOrderStatusText(OrderStatusTextUtil.statusToText(marketOrder.getOrderStatus()));
                MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria orderGoodsCriteria = marketOrderGoodsExample.createCriteria();
                if (marketOrder.getId() != null) {
                    orderGoodsCriteria.andOrderIdEqualTo(marketOrder.getId());
                }
                List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
                orderWxListVO.setGoodsList(marketOrderGoods);
                HandleOption handleOption = new HandleOption();
                if (marketOrder.getAftersaleStatus() == 0) {
                    handleOption.setAftersale(false);
                } else if (marketOrder.getAftersaleStatus() == 1) {
                    handleOption.setAftersale(true);
                }
                handleOption.setComment(true);
                handleOption.setDelete(marketOrder.getDeleted());
                handleOption.setConfirm(false);
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
                orderWxListVO.setHandleOption(handleOption);
                orderWxListVOS.add(orderWxListVO);
            }
            // 获取分页信息
            PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
            // 封装数据返回结果
            CommonData<OrderWxListVO> orderWxListVOCommonData = new CommonData<>();
            orderWxListVOCommonData.setLimit(pageInfo.getPageSize());
            orderWxListVOCommonData.setList(orderWxListVOS);
            orderWxListVOCommonData.setPage(pageInfo.getPageNum());
            orderWxListVOCommonData.setPages(pageInfo.getPages());
            orderWxListVOCommonData.setTotal((int) pageInfo.getTotal());
            return orderWxListVOCommonData;
        } else if (showType == 1) {
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
            criteria.andOrderStatusEqualTo((short) 101);
            criteria.andDeletedEqualTo(false);
            List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
            for (MarketOrder marketOrder : marketOrders) {
                OrderWxListVO orderWxListVO = new OrderWxListVO();
                orderWxListVO.setId(marketOrder.getId());
                orderWxListVO.setActualPrice(marketOrder.getActualPrice());
                orderWxListVO.setAftersaleStatus(marketOrder.getAftersaleStatus());
                orderWxListVO.setIsGroupin(false);
                orderWxListVO.setOrderSn(marketOrder.getOrderSn());
                orderWxListVO.setOrderStatusText(OrderStatusTextUtil.statusToText(marketOrder.getOrderStatus()));
                MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria orderGoodsCriteria = marketOrderGoodsExample.createCriteria();
                if (marketOrder.getId() != null) {
                    orderGoodsCriteria.andOrderIdEqualTo(marketOrder.getId());
                }
                List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
                orderWxListVO.setGoodsList(marketOrderGoods);
                HandleOption handleOption = new HandleOption();
                if (marketOrder.getAftersaleStatus() == 0) {
                    handleOption.setAftersale(false);
                } else if (marketOrder.getAftersaleStatus() == 1) {
                    handleOption.setAftersale(true);
                }
                handleOption.setComment(true);
                handleOption.setDelete(marketOrder.getDeleted());
                handleOption.setConfirm(false);
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
                orderWxListVO.setHandleOption(handleOption);
                orderWxListVOS.add(orderWxListVO);
            }
            // 获取分页信息
            PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
            // 封装数据返回结果
            CommonData<OrderWxListVO> orderWxListVOCommonData = new CommonData<>();
            orderWxListVOCommonData.setLimit(pageInfo.getPageSize());
            orderWxListVOCommonData.setList(orderWxListVOS);
            orderWxListVOCommonData.setPage(pageInfo.getPageNum());
            orderWxListVOCommonData.setPages(pageInfo.getPages());
            orderWxListVOCommonData.setTotal((int) pageInfo.getTotal());
            return orderWxListVOCommonData;
        } else if (showType == 2) {
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
            criteria.andOrderStatusEqualTo((short) 201);
            criteria.andDeletedEqualTo(false);
            List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
            for (MarketOrder marketOrder : marketOrders) {
                OrderWxListVO orderWxListVO = new OrderWxListVO();
                orderWxListVO.setId(marketOrder.getId());
                orderWxListVO.setActualPrice(marketOrder.getActualPrice());
                orderWxListVO.setAftersaleStatus(marketOrder.getAftersaleStatus());
                orderWxListVO.setIsGroupin(false);
                orderWxListVO.setOrderSn(marketOrder.getOrderSn());
                orderWxListVO.setOrderStatusText(OrderStatusTextUtil.statusToText(marketOrder.getOrderStatus()));
                MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria orderGoodsCriteria = marketOrderGoodsExample.createCriteria();
                if (marketOrder.getId() != null) {
                    orderGoodsCriteria.andOrderIdEqualTo(marketOrder.getId());
                }
                List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
                orderWxListVO.setGoodsList(marketOrderGoods);
                HandleOption handleOption = new HandleOption();
                if (marketOrder.getAftersaleStatus() == 0) {
                    handleOption.setAftersale(false);
                } else if (marketOrder.getAftersaleStatus() == 1) {
                    handleOption.setAftersale(true);
                }
                handleOption.setComment(true);
                handleOption.setDelete(marketOrder.getDeleted());
                handleOption.setConfirm(false);
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
                orderWxListVO.setHandleOption(handleOption);
                orderWxListVOS.add(orderWxListVO);
            }
            // 获取分页信息
            PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
            // 封装数据返回结果
            CommonData<OrderWxListVO> orderWxListVOCommonData = new CommonData<>();
            orderWxListVOCommonData.setLimit(pageInfo.getPageSize());
            orderWxListVOCommonData.setList(orderWxListVOS);
            orderWxListVOCommonData.setPage(pageInfo.getPageNum());
            orderWxListVOCommonData.setPages(pageInfo.getPages());
            orderWxListVOCommonData.setTotal((int) pageInfo.getTotal());
            return orderWxListVOCommonData;
        } else if (showType == 3) {
            MarketOrderExample marketOrderExample = new MarketOrderExample();
            MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
            criteria.andOrderStatusEqualTo((short) 301);
            criteria.andDeletedEqualTo(false);
            List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
            for (MarketOrder marketOrder : marketOrders) {
                OrderWxListVO orderWxListVO = new OrderWxListVO();
                orderWxListVO.setId(marketOrder.getId());
                orderWxListVO.setActualPrice(marketOrder.getActualPrice());
                orderWxListVO.setAftersaleStatus(marketOrder.getAftersaleStatus());
                orderWxListVO.setIsGroupin(false);
                orderWxListVO.setOrderSn(marketOrder.getOrderSn());
                orderWxListVO.setOrderStatusText(OrderStatusTextUtil.statusToText(marketOrder.getOrderStatus()));
                MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
                MarketOrderGoodsExample.Criteria orderGoodsCriteria = marketOrderGoodsExample.createCriteria();
                if (marketOrder.getId() != null) {
                    orderGoodsCriteria.andOrderIdEqualTo(marketOrder.getId());
                }
                List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
                orderWxListVO.setGoodsList(marketOrderGoods);
                HandleOption handleOption = new HandleOption();
                if (marketOrder.getAftersaleStatus() == 0) {
                    handleOption.setAftersale(false);
                } else if (marketOrder.getAftersaleStatus() == 1) {
                    handleOption.setAftersale(true);
                }
                handleOption.setComment(true);
                handleOption.setDelete(marketOrder.getDeleted());
                handleOption.setConfirm(false);
                handleOption.setCancel(false);
                handleOption.setPay(false);
                handleOption.setRebuy(false);
                handleOption.setRefund(false);
                orderWxListVO.setHandleOption(handleOption);
                orderWxListVOS.add(orderWxListVO);
            }
            // 获取分页信息
            PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
            // 封装数据返回结果
            CommonData<OrderWxListVO> orderWxListVOCommonData = new CommonData<>();
            orderWxListVOCommonData.setLimit(pageInfo.getPageSize());
            orderWxListVOCommonData.setList(orderWxListVOS);
            orderWxListVOCommonData.setPage(pageInfo.getPageNum());
            orderWxListVOCommonData.setPages(pageInfo.getPages());
            orderWxListVOCommonData.setTotal((int) pageInfo.getTotal());
            return orderWxListVOCommonData;
        }
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        ArrayList<Short> shorts = new ArrayList<>();
        shorts.add((short) 401);
        shorts.add((short) 402);
        criteria.andOrderStatusIn(shorts);
        criteria.andDeletedEqualTo(false);
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        for (MarketOrder marketOrder : marketOrders) {
            OrderWxListVO orderWxListVO = new OrderWxListVO();
            orderWxListVO.setId(marketOrder.getId());
            orderWxListVO.setActualPrice(marketOrder.getActualPrice());
            orderWxListVO.setAftersaleStatus(marketOrder.getAftersaleStatus());
            orderWxListVO.setIsGroupin(false);
            orderWxListVO.setOrderSn(marketOrder.getOrderSn());
            orderWxListVO.setOrderStatusText(OrderStatusTextUtil.statusToText(marketOrder.getOrderStatus()));
            MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
            MarketOrderGoodsExample.Criteria orderGoodsCriteria = marketOrderGoodsExample.createCriteria();
            if (marketOrder.getId() != null) {
                orderGoodsCriteria.andOrderIdEqualTo(marketOrder.getId());
            }
            List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
            orderWxListVO.setGoodsList(marketOrderGoods);
            HandleOption handleOption = new HandleOption();
            if (marketOrder.getAftersaleStatus() == 0) {
                handleOption.setAftersale(false);
            } else if (marketOrder.getAftersaleStatus() == 1) {
                handleOption.setAftersale(true);
            }
            handleOption.setComment(true);
            handleOption.setDelete(marketOrder.getDeleted());
            handleOption.setConfirm(false);
            handleOption.setCancel(false);
            handleOption.setPay(false);
            handleOption.setRebuy(false);
            handleOption.setRefund(false);
            orderWxListVO.setHandleOption(handleOption);
            orderWxListVOS.add(orderWxListVO);
        }
        // 获取分页信息
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);
        // 封装数据返回结果
        CommonData<OrderWxListVO> orderWxListVOCommonData = new CommonData<>();
        orderWxListVOCommonData.setLimit(pageInfo.getPageSize());
        orderWxListVOCommonData.setList(orderWxListVOS);
        orderWxListVOCommonData.setPage(pageInfo.getPageNum());
        orderWxListVOCommonData.setPages(pageInfo.getPages());
        orderWxListVOCommonData.setTotal((int) pageInfo.getTotal());
        return orderWxListVOCommonData;
    }

    @Override
    public OrderWxDetailVO selectOrderDetail(Integer orderId) {
        OrderWxDetailVO orderWxDetailVO = new OrderWxDetailVO();
        ArrayList<ExpressInfo> expressInfos = new ArrayList<>();
        orderWxDetailVO.setExpressInfo(expressInfos);

        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria orderGoodsCriteria = marketOrderGoodsExample.createCriteria();
        if (orderId != null) {
            orderGoodsCriteria.andOrderIdEqualTo(orderId);
        }
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        orderWxDetailVO.setOrderGoods(marketOrderGoods);
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);

        //新建OrderWxDetailInfoVo并赋值
        OrderWxDetailInfoVo orderWxDetailInfoVo = new OrderWxDetailInfoVo();
        orderWxDetailInfoVo.setActualPrice(marketOrder.getActualPrice());
        orderWxDetailInfoVo.setAddTime(marketOrder.getAddTime());
        orderWxDetailInfoVo.setAddress(marketOrder.getAddress());
        orderWxDetailInfoVo.setAftersaleStatus(marketOrder.getAftersaleStatus());
        orderWxDetailInfoVo.setConsignee(marketOrder.getConsignee());
        orderWxDetailInfoVo.setCouponPrice(marketOrder.getCouponPrice());
        orderWxDetailInfoVo.setFreightPrice(marketOrder.getFreightPrice());
        orderWxDetailInfoVo.setGoodsPrice(marketOrder.getGoodsPrice());
        orderWxDetailInfoVo.setId(orderId);
        orderWxDetailInfoVo.setMessage(marketOrder.getMessage());
        orderWxDetailInfoVo.setMobile(marketOrder.getMobile());
        orderWxDetailInfoVo.setOrderSn(marketOrder.getOrderSn());
        orderWxDetailInfoVo.setOrderStatusText(OrderStatusTextUtil.statusToText(marketOrder.getOrderStatus()));
        HandleOption handleOption = new HandleOption();
        handleOption.setAftersale(false);
        handleOption.setComment(false);
        handleOption.setDelete(false);
        handleOption.setRebuy(false);
        handleOption.setCancel(false);
        handleOption.setPay(false);
        handleOption.setRefund(false);
        handleOption.setConfirm(false);


        if (marketOrder.getOrderStatus() == 101) {
            handleOption.setCancel(true);
            handleOption.setPay(true);
        }
        if (marketOrder.getOrderStatus() == 102 || marketOrder.getOrderStatus() == 103 || marketOrder.getOrderStatus() == 203) {
            handleOption.setDelete(true);
        }

        if (marketOrder.getOrderStatus() == 201) {
            handleOption.setRefund(true);
        }

        if (marketOrder.getOrderStatus() == 301) {
            handleOption.setConfirm(true);
        }

        if (marketOrder.getOrderStatus() == 401 || marketOrder.getOrderStatus() == 402) {
            handleOption.setAftersale(true);
            handleOption.setComment(true);
            handleOption.setDelete(true);
            handleOption.setRebuy(true);
        }
        orderWxDetailInfoVo.setHandleOption(handleOption);
        orderWxDetailVO.setOrderInfo(orderWxDetailInfoVo);
        return orderWxDetailVO;
    }

    @Override
    public void updateOrderRefund(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setOrderStatus((short) 202);
        marketOrder.setUpdateTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return;
    }

    @Override
    public void deleteOrder(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setUpdateTime(date);
        marketOrder.setDeleted(true);
        marketOrder.setEndTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);

    }

    @Override
    public void confirmOrder(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setConfirmTime(date);
        marketOrder.setUpdateTime(date);
        marketOrder.setOrderStatus((short) 401);
        Integer goodsTypeNum = marketOrderGoodsMapper.selectGoodsTypeNumByOrderId(orderId);
        marketOrder.setComments(goodsTypeNum);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    @Override
    public void cancelOrder(Integer orderId) {
        MarketOrder marketOrder = new MarketOrder();
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setId(orderId);
        marketOrder.setOrderStatus((short) 102);
        marketOrder.setUpdateTime(date);
        marketOrder.setEndTime(date);
        marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
    }

    @Override
    public MarketOrderGoods selectOrderGoods(Integer orderId, Integer goodsId) {
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        if (orderId != null) {
            criteria.andOrderIdEqualTo(orderId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<MarketOrderGoods> orderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        MarketOrderGoods marketOrderGoods = orderGoods.get(0);
        return marketOrderGoods;
    }

    @Override
    public void insertComment(OrderCommentBO orderCommentBO) {
        MarketComment marketComment = new MarketComment();
        Date date = new Date(System.currentTimeMillis());
        Integer goodsId = marketOrderGoodsMapper.selectGoodIdByOrderGoods(orderCommentBO.getOrderGoodsId());
        Integer orderId = marketOrderGoodsMapper.selectOrderIdByOrderGoodsId(orderCommentBO.getOrderGoodsId());
        Integer userId = marketOrderMapper.selectUserIdByOrderId(orderId);
        marketComment.setContent(orderCommentBO.getContent());
        marketComment.setHasPicture(orderCommentBO.getHasPicture());
        marketComment.setPicUrls(orderCommentBO.getPicUrls());
        marketComment.setStar(orderCommentBO.getStar());
        marketComment.setValueId(goodsId);
        marketComment.setType((byte) 0);
        marketComment.setUserId(userId);
        marketComment.setAddTime(date);
        marketComment.setUpdateTime(date);
        marketComment.setDeleted(false);
        marketCommentMapper.insertSelective(marketComment);
        MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
        marketOrderGoods.setId(orderCommentBO.getOrderGoodsId());
        marketOrderGoods.setComment(marketComment.getId());
        marketOrderGoodsMapper.updateByPrimaryKeySelective(marketOrderGoods);
        marketOrderMapper.updateCommentByOrderId(orderId);
    }

    @Override
    public OrderSubmitVO insertOrder(OrderSubmitBO orderSubmitBO) {
        OrderSubmitVO orderSubmitVO = new OrderSubmitVO();

        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setMessage(orderSubmitBO.getMessage());


        //通过addressId查询数据封装到marketOrder和marketOrderGoods对象
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(orderSubmitBO.getAddressId());

        marketOrder.setUserId(marketAddress.getUserId());
        marketOrder.setMobile(marketAddress.getTel());
        marketOrder.setOrderStatus((short) 201);
        marketOrder.setConsignee(marketAddress.getName());
        marketOrder.setAddress(marketAddress.getAddressDetail());
        Date date = new Date(System.currentTimeMillis());
        marketOrder.setAddTime(date);
        marketOrder.setUpdateTime(date);


        //通过cartId查询数据封装到marketOrder对象
        // MarketCart marketCart = marketCartMapper.selectByPrimaryKey(orderSubmitBO.getCartId());
//        marketOrder.setGoodsPrice(marketCart.getPrice());

        //通过couponId查询数据封装到marketOrder对象

            MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(orderSubmitBO.getCouponId());

            marketOrder.setCouponPrice(marketCoupon==null?BigDecimal.valueOf(0):marketCoupon.getDiscount());


        // marketGroupon
        MarketGroupon marketGroupon = marketGrouponMapper.selectByPrimaryKey(orderSubmitBO.getGrouponLinkId());

        // marketGrouponRules

            MarketGrouponRules marketGrouponRules = marketGrouponRulesMapper.selectByPrimaryKey(orderSubmitBO.getGrouponRulesId());
            marketOrder.setGrouponPrice(marketGrouponRules==null?BigDecimal.valueOf(0):marketGrouponRules.getDiscount());




        MarketCartExample marketCartExample = new MarketCartExample();
        MarketCartExample.Criteria criteria1 = marketCartExample.createCriteria();
        criteria1.andDeletedEqualTo(false).andCheckedEqualTo(true).andUserIdEqualTo(marketAddress.getUserId());
        if (orderSubmitBO.getCartId() != 0) {
            criteria1.andIdEqualTo(orderSubmitBO.getCartId());
        }

        List<MarketCart> marketCarts = marketCartMapper.selectByExample(marketCartExample);

        // goodsPrice
        Double checkedGoodsAmount = marketCartMapper.checkedGoodsAmount(marketAddress.getUserId());
        marketOrder.setGoodsPrice(checkedGoodsAmount==null?BigDecimal.valueOf(0):BigDecimal.valueOf(checkedGoodsAmount));
        marketOrder.setOrderPrice(checkedGoodsAmount==null?BigDecimal.valueOf(0):BigDecimal.valueOf(checkedGoodsAmount));
        marketOrder.setIntegralPrice(BigDecimal.valueOf(0));
        // 查询运费信息
        ArrayList<String> list = new ArrayList<>();
        list.add("market_express_freight_min");
        list.add("market_express_freight_value");

        HashMap<String, String> map = new HashMap<>();
        for (String keyName : list) {
            MarketSystemExample example = new MarketSystemExample();
            MarketSystemExample.Criteria criteria = example.createCriteria();
            criteria.andKeyNameEqualTo(keyName);

            List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(example);

            map.put(keyName, marketSystems.get(0).getKeyValue());
        }
        if (marketOrder.getGoodsPrice().compareTo(BigDecimal.valueOf(Double.parseDouble(map.get("market_express_freight_min")))) >= 0) {
            marketOrder.setFreightPrice(BigDecimal.valueOf(0));
        } else {
            marketOrder.setFreightPrice(BigDecimal.valueOf(Double.parseDouble(map.get("market_express_freight_value"))));
        }
        marketOrder.setPayTime(date);
        // actualPrice
        marketOrder.setActualPrice(marketOrder.getGoodsPrice().subtract(
                marketOrder.getCouponPrice()).subtract(
                marketOrder.getGrouponPrice()).add(
                marketOrder.getFreightPrice()));

        marketOrder.setOrderSn("1");
        //向数据库插入Order和Ordergoods数据
        marketOrderMapper.insertSelective(marketOrder);
        marketOrderMapper.updateOrdersnByOrderId(marketOrder.getId());

        for (MarketCart cart : marketCarts) {
            MarketOrderGoods marketOrderGoods = new MarketOrderGoods();
            marketOrderGoods.setAddTime(date);
            marketOrderGoods.setUpdateTime(date);

            marketOrderGoods.setOrderId(marketOrder.getId());
            marketOrderGoods.setGoodsName(cart.getGoodsName());
            marketOrderGoods.setGoodsSn(cart.getGoodsSn());
            marketOrderGoods.setProductId(cart.getProductId());
            marketOrderGoods.setNumber(cart.getNumber());
            marketOrderGoods.setSpecifications(cart.getSpecifications());
            marketOrderGoods.setPicUrl(cart.getPicUrl());
            marketOrderGoods.setGoodsId(cart.getGoodsId());
            marketOrderGoodsMapper.insertSelective(marketOrderGoods);
            cart.setUpdateTime(date);
            marketCartMapper.updateDeletedByCartId(cart.getId(),cart.getUpdateTime());

        }

        orderSubmitVO.setOrderId(marketOrder.getId());
        orderSubmitVO.setGrouponLinkId(orderSubmitBO.getGrouponLinkId());
        // 需要 删除 这条购物车记录
        return orderSubmitVO;
    }
}
